package test.http;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.startup.Tomcat;

import java.util.concurrent.TimeUnit;

public class TomcatServletServer {

    public static void main(String[] args) throws LifecycleException {
        long start = System.nanoTime();
        Tomcat tomcat = new Tomcat();
        String mWorkingDir = System.getProperty("java.io.tmpdir");
        tomcat.setBaseDir(mWorkingDir);
        Connector connector = new Connector("org.apache.coyote.http11.Http11Nio2Protocol");
        connector.setPort(8080);
        tomcat.setConnector(connector);
        Context ctx = tomcat.addContext("", null);
        Tomcat.addServlet(ctx, "plain-text", new PlainTextServlet());
        ctx.addServletMappingDecoded("/plain-text", "plain-text", false);
        tomcat.start();
        long end = System.nanoTime();
        System.out.println("Started in " + TimeUnit.NANOSECONDS.toMillis(end - start) + "ms");
        tomcat.getServer().await();
    }
}
